<?php
//error_log("ran\n", 3, "/var/www/vhosts/vps4291.unoeuro-server.com/httpdocs/_cj/errors.log");
define('DS', DIRECTORY_SEPARATOR);
define('_VEXEC', 1);
define('JPATH_PLATFORM', dirname(__FILE__));
define('JPATH_BASE', dirname(__FILE__));


/*DEBUG
ini_set("display_errors", 1);
error_reporting(E_ALL); 
//DELETE FROM `efdsh_user_payment` a where a.id_user NOT IN (SELECT id FROM `efdsh_users`)
//*/


//Joomla db handlers
include(JPATH_BASE.DS."..".DS."libraries".DS."loader.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."log".DS."log.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."log".DS."entry.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."utilities".DS."date.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."error".DS."error.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."methods.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."factory.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."registry".DS."registry.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."base".DS."object.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."language".DS."language.php");
include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."database".DS."exception.php");
	include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."database".DS."database.php");
		include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."database".DS."database".DS."mysql.php");
			include(JPATH_BASE.DS."..".DS."libraries".DS."joomla".DS."database".DS."database".DS."mysqli.php");

	function mySql2PhpTime($sqlDate){
		$arr = date_parse($sqlDate);
		return mktime($arr["hour"],$arr["minute"],$arr["second"],$arr["month"],$arr["day"],$arr["year"]);
	}

	function php2MySqlTime($date){
		return date("Y-m-d H:i:00", $date);
	}

$db=JDatabase::getInstance(array(
	'driver' => "mysqli",
	'host' => "localhost",
	'user' => "fpdbu",
	'password' => "6PKPRKK74wEDdzPD",
	'database' => "fitplan_dk",
	'prefix' => "efdsh_"
	));

//INIT
date_default_timezone_set("Europe/Copenhagen");
include("qpapi.php");

$qp = new Quickpay('33760310', '9ead3e386702116bc6aec0819a668e0d0902a6e728e85fe16002d591f37564d8');
//$qp->testmode(TRUE);

$db->setQuery("SELECT p.*, u.registerDate FROM `#__user_payment` p
JOIN `#__users` u
ON p.id_user=u.id
WHERE u.block=0 AND p.tid <> 0 AND p.id_option <> 0");
$rows=$db->loadObjectList();
$now=time();
$now=strtotime(date("Y-m-d", $now));

//MAIN flow
//Quickpay::recurring( $ordernumber, $amount, $currency, $transaction, [$autocapture = FALSE] )
//ordernumber, amount*100, 'DKK', transaction
if($rows)
	foreach($rows as $r)
	{
		$regdate=strtotime($r->registerDate);
		$firstpay=strtotime(date("Y-m-d", $regdate)." +14days");
		$days=($now-$firstpay)/86400;
		if($firstpay==$now){
			$res=$qp->recurring(date('m', $now).'u'.$r->id_user, $r->total*100, 'DKK', $r->tid, TRUE);
			echo "[transaction:",$r->tid,",status:",$res->qpstatmsg,",is_valid:",$res->is_valid,"]";
		}
		elseif( $days%30 ==0 )
		{
			$res=$qp->recurring(date('m', $now).'u'.$r->id_user, $r->id_option*100, 'DKK', $r->tid, TRUE);
			echo "[transaction:",$r->tid,",status:",$res->qpstatmsg,",is_valid:",$res->is_valid,"]";
		}
	}
else
	echo "FALSE";
///MAIN